//
//  BRHudLoadingView.m
//  BrezzeSaaS
//
//  Created by IOS on 2021/9/17.
//  Copyright © 2021 Brezze GWG. All rights reserved.
//

#import "BRHudLoadingView.h"

@interface BRHudLoadingView ()
@property (nonatomic,strong) NSMutableArray *idleImages;
@end

@implementation BRHudLoadingView

/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/
- (instancetype)initWithFrame:(CGRect)frame {
    if (self = [super initWithFrame:frame]) {
        [self initUI];
    }
    return self;
}

-(void)initUI{
    
    [self addSubview:self.animationImageView];
    
    [self.animationImageView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.offset(28);
        make.width.mas_equalTo(42);
        make.height.mas_equalTo(30);
        make.centerX.offset(0);
    }];
    
    [self addSubview:self.titleL];
    
    [self.titleL mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.offset(28);
        make.right.offset(-28);
        make.width.mas_equalTo(164);
        make.bottom.offset(-16);
        make.top.equalTo(self.animationImageView.mas_bottom).offset(12);
    }];
    
    self.layer.cornerRadius = 10;
    self.layer.masksToBounds = YES;
}

- (void)dealloc {
    [self stop];
}

- (void)setTitle:(NSString *)title {
    [self br_setTitle:title img:@""];
}

- (void)br_setTitle:(NSString *)title img:(NSString *)imgStr {
    _title = title;
    self.titleL.text = title;
    CGSize labelSize = [self.titleL sizeThatFits:CGSizeMake(164, 300)];
    CGFloat w = 220;
    self.mj_w = w;
    self.mj_h = 86 + labelSize.height;
    if (imgStr.length != 0) self.animationImageView.image = UIImageMake(imgStr);

}

- (QMUILabel *)titleL {
    
    if (_titleL == nil) {
        _titleL = [BRToolsView br_label:@"--" textColor:UIColor.whiteColor font:[UIFont br_fontR:14]];
        _titleL.textAlignment = NSTextAlignmentCenter;
        _titleL.numberOfLines = 0;
    }
    return _titleL;
}
-(UIImageView *)animationImageView
{
    if (!_animationImageView) {
        _animationImageView= [[UIImageView alloc]initWithFrame:CGRectMake(0,0, 42, 30)];
//        _animationImageView.center = CGPointMake(self.frame.size.width/2, self.frame.size.height/2);
    }
    return _animationImageView;
}

-(NSMutableArray *)idleImages{
    if (!_idleImages) {
        _idleImages= [NSMutableArray array];
        for (int i = 1; i <= 77; i++) {
            YYImage *image = [YYImage imageNamed:[NSString stringWithFormat:@"loading_%2d", i]];
            if (image) [_idleImages addObject:image];
            
        }
    }
    return _idleImages;
}

-(void)start
{
    self.animationImageView.animationImages = self.idleImages;
    self.animationImageView.animationDuration = 1.17;//动画时间
    self.animationImageView.animationRepeatCount = 0;//动画重复次数，0：无限
    [self.animationImageView startAnimating];//开始动画
    
}

-(void)stop{
    [self.animationImageView stopAnimating];
}



@end
